import { TMakePlugin, PluginAPI, PluginLifecycleEvent } from '../plugin-system';
import { TMakeASTNode } from '../types';


const advancedBuildPlugin: TMakePlugin = {
  metadata: {
    name: 'advanced-build-plugin',
    version: '1.0.0',
    description: '提供高级构建功能，包括优化设置、自定义编译标志和环境检测',
    author: 'TMake Team'
  },

  init(api: PluginAPI) {
    
    /**
     * 设置高级优化选项
     * @description 启用编译器的高级优化功能，包括函数内联和激进内联
     * @returns {string} 返回确认消息
     */
    api.registerFunction('设置高级优化', (node: TMakeASTNode) => {
      const config = api.getConfig();
      config.optimizeLevel = 3;
      
      if (!config.variables) {
        config.variables = {};
      }
      
      config.variables.advancedOptimizations = true;
      config.variables.inlineFunctions = true;
      config.variables.aggressiveInlining = true;
      
      return '高级优化已启用';
    });

    
    /**
     * 添加自定义编译标志
     * @description 向编译器配置中添加自定义的编译标志
     * @param {Array<string>} flags 要添加的编译标志数组
     * @returns {Array<string>} 返回已添加的编译标志数组
     */
    api.registerFunction('添加编译标志', (node: TMakeASTNode, context: any) => {
      const flags = node.params?.map((param: TMakeASTNode) => {
        const flag = param.value;
        if (typeof flag !== 'string') {
          throw new Error('编译标志必须是字符串');
        }
        return flag;
      }) || [];
      
      const config = api.getConfig();
      if (!config.variables) {
        config.variables = {};
      }
      if (!config.variables.customFlags) {
        config.variables.customFlags = [];
      }
      config.variables.customFlags.push(...flags);
      
      return flags;
    });

    
    /**
     * 检测环境变量
     * @description 检测指定的环境变量并将其存储在配置中
     * @param {Array<string>} envVars 要检测的环境变量名数组
     * @returns {Record<string, string>} 返回环境变量名和值的映射
     */
    api.registerFunction('检测环境', (node: TMakeASTNode, context: any) => {
      const envVars = node.params?.map((param: TMakeASTNode) => {
        const envVar = param.value;
        if (typeof envVar !== 'string') {
          throw new Error('环境变量名必须是字符串');
        }
        return envVar;
      }) || [];
      
      const envInfo: Record<string, string> = {};
      for (const envVar of envVars) {
        envInfo[envVar] = process.env[envVar] || '';
      }
      
      const config = api.getConfig();
      if (!config.variables) {
        config.variables = {};
      }
      config.variables.environment = envInfo;
      
      return envInfo;
    });
  },

  
  [PluginLifecycleEvent.AFTER_INIT]: function(api: PluginAPI) {
    // 移除了初始化日志
  },

  [PluginLifecycleEvent.BEFORE_BUILD]: function(api: PluginAPI, options: any) {
    
    const config = api.getConfig();
    if (config.variables?.advancedOptimizations) {
        // 移除了优化设置日志
        
        if (!options.optimize || options.optimize < 3) {
          options.optimize = 3;
        }
      }
      
      if (config.variables?.customFlags && Array.isArray(config.variables.customFlags)) {
        // 移除了编译标志日志
        
        if (!options.customArgs) {
          options.customArgs = [];
        }
        options.customArgs.push(...config.variables.customFlags);
      }
  },

  [PluginLifecycleEvent.AFTER_BUILD]: function(api: PluginAPI, success: boolean) {
    // 移除了构建结果日志
  }
};

export default advancedBuildPlugin;